package com.example.takeaway.controller;

import com.example.takeaway.entity.Banners;
import com.example.takeaway.serivce.IBannersService;
import com.example.takeaway.utils.PageResult;
import com.example.takeaway.utils.PassToken;
import com.example.takeaway.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 轮播图Controller
 * 
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/system/banners")
public class BannersController
{
    @Autowired
    private IBannersService bannersService;

    /**
     * 查询轮播图列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Banners banners, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
        // startPage();

        PageResult pageResult = bannersService.selectBannersList(banners,pageNum,pageSize);
        return  ServerResponse.createBysuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出轮播图列表
     */


//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Banners banners)
//    {
//        List<Banners> list = bannersService.selectBannersList(banners);
//        ExcelUtil<Banners> util = new ExcelUtil<Banners>(Banners.class);
//        util.exportExcel(response, list, "轮播图数据");
//    }

    /**
     * 获取轮播图详细信息
     */
    @PassToken
    @GetMapping(value = "/{id}")
    public ServerResponse getInfo(@PathVariable("id") Long id)
    {
        Banners banners = bannersService.selectBannersById(id);
        return ServerResponse.createBysuccess(200,banners,"查询成功");
    }

    /**
     * 新增轮播图
     */
//    @PreAuthorize("@ss.hasPermi('system:banners:add')")
//    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public ServerResponse add(@RequestBody Banners banners)
    {
        banners.setCreateTime(new Date());
        banners.setCreateBy("admin");
        int i = bannersService.insertBanners(banners);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
//        return toAjax(bannersService.insertBanners(banners));
    }

    /**
     * 修改轮播图
     */
//    @PreAuthorize("@ss.hasPermi('system:banners:edit')")
//    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public ServerResponse edit(@RequestBody Banners banners)
    {
        banners.setUpdateTime(new Date());
        banners.setUpdateBy("admin");
        int i = bannersService.updateBanners(banners);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","修改成功");
        }else{
            return  ServerResponse.createByfail(400,"修改失败");
        }
//        return toAjax(bannersService.updateBanners(banners));
    }

    /**
     * 删除轮播图
     */
//    @PreAuthorize("@ss.hasPermi('system:banners:remove')")
//    @Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ServerResponse remove(@PathVariable Long[] ids)
    {
        int i = bannersService.deleteBannersByIds(ids);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","删除成功");
        }else{
            return  ServerResponse.createByfail(400,"删除失败");
        }

    }
}
